package com.jsy.mini12306.travel.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.jsy.mini12306.travel.config.OssConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/5
 */

@Service
public class OssService {

    @Resource
    OssConfig ossConfig;


    public List<String> getHomePageImages() {
        ArrayList<String> urls = new ArrayList<>();
        String protocol = "https://";
        String bucket = ossConfig.getBucket();
        String endPoint = ossConfig.getEndpoint();
        String preFix = "front-vue-static-resources/travel-images";
        OSS ossClient = ossConfig.getOssClient();
        ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(bucket).withPrefix(preFix));
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            String url = protocol + bucket + "." + endPoint + "/" + s.getKey();
            urls.add(url);
        }
        return urls;
    }
}
